#include <server.h>
#include <common.h>
#include <stdlib.h>
#include <time.h>
#include <mqtt_client.h>
#include <pthread.h>
#include <sys/time.h>
#include <system.h>
#include <firmware.h>
#include <http_download.h>

#define ARRAYSIZE(a)	(sizeof(a) / sizeof(a[0]))

void gw_publish_setting_rsp_data(char *msg, int msglen)
{
	char topic[128] = {0};

	if(mqtt_client_connect_ok()) {
		DEBUG("control_rsp:%s", msg);
		sprintf(topic, "/%s/respond/control", get_gw_sn());
		mqtt_client_publish_data(topic, (unsigned char *)msg, msglen);
	}
}

static void setting_rsp(char *command, int ret)
{
	char *out;
	cJSON *root;
	root = cJSON_CreateObject();
	cJSON_AddNumberToObject(root, "id", generate_msgid());
	cJSON_AddStringToObject(root, "gwid", get_gw_sn());
	cJSON_AddStringToObject(root, "command", command);
	cJSON_AddNumberToObject(root, "ret", ret);
	out = cJSON_PrintUnformatted(root);
	cJSON_Delete(root);
	gw_publish_setting_rsp_data(out, strlen(out));
	free(out);
}

static void firmware_upgrade_req(CLOUD_DATA *cloud_data)
{
	cloud_firmware_upgrade_req(cloud_data);
}

static void natfrp_start_req(CLOUD_DATA *cloud_data)
{
	myexec("/oem/init.d/natfrp start", NULL, NULL);
	setting_rsp(cloud_data->command, 0);
}

static void natfrp_stop_req(CLOUD_DATA *cloud_data)
{
	myexec("/oem/init.d/natfrp stop", NULL, NULL);
	setting_rsp(cloud_data->command, 0);
}

static CLOUD_CMD cmd_parse_array[] = {
	{ "upgrade", firmware_upgrade_req },
	{ "natfrp_start", natfrp_start_req },
	{ "natfrp_stop", natfrp_stop_req },
};

static void cloud_data_free(CLOUD_DATA *cloud_data)
{
	cJSON_Delete(cloud_data->root);
}

static int cloud_data_parse(char *buf, int len, CLOUD_DATA *cloud_data)
{
	cJSON *value;
	char *out;

	memset(cloud_data, 0, sizeof(CLOUD_DATA));
	cloud_data->root = cJSON_Parse(buf);
	if(cloud_data->root == NULL)
	{
		DEBUG("cJSON_Parse error %s", buf); 
		goto invalid_command;
	}

	out = cJSON_PrintUnformatted(cloud_data->root);
	DEBUG("%d,%s", len, out);
	free(out);

	value = cJSON_GetObjectItem(cloud_data->root, "gwid");
	if(value)
	{
		cloud_data->gwid = value->valuestring;
	}

	cloud_data->data = cJSON_GetObjectItem(cloud_data->root, "data");

	value = cJSON_GetObjectItem(cloud_data->root, "command");
	if(value)
	{
		cloud_data->command = value->valuestring;
	}
	else
	{
		value = cJSON_GetObjectItem(cloud_data->root, "action");
		if(value)
		{
			cloud_data->command = value->valuestring;
		}
		else
		{
			goto invalid_command;	//即没有 "command" 字段,	也没有 "action" 字段 退出处理
		}
	}

	return 0;

invalid_command:
	cloud_data_free(cloud_data);
	return -1;
}


static void cloud_data_handle(CLOUD_DATA *cloud_data)
{
	int i = 0;

	for(i=0; i<ARRAYSIZE(cmd_parse_array); i++) {
		if(strcmp(cmd_parse_array[i].command, cloud_data->command) == 0) {
			if(cloud_data->gwid && (strcmp(get_gw_sn(), cloud_data->gwid) == 0 || strcmp("all", cloud_data->gwid) == 0)) {
				if(cmd_parse_array[i].local_func) {
					cmd_parse_array[i].local_func(cloud_data);
				}
			}
			return;
		}
	}
}

static int handle_server_message(char *topic, void *buf, int len)
{
	CLOUD_DATA cloud_data = {0};

	if(cloud_data_parse(buf, len, &cloud_data)) {
		return -1;
	}
	
	cloud_data_handle(&cloud_data);
	cloud_data_free(&cloud_data);
	return 0;
}

//解析平台json数据
int handle_server_data(char *topic, void *buf, int len)
{
	handle_server_message(topic, buf, len);
	return 0;
}

